﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace benefit.Controllers
{
    public class CompanyController : BenefitHelper.Tools.BaseControl
    {
        //
        // GET: /Company/

        public ActionResult Index(int uid, string pdate)
        {
            ViewBag.queryyear = pdate.Substring(0, 4);
            ViewBag.querymonth = pdate.Substring(4, 2);
            ViewBag.queryday = pdate.Substring(6, 2);
            ViewBag.pdate = pdate;
            if (BenefitHelper.Sys.Company.HasInited(db))
            {
                ViewBag.company = db.Company.First();
            }
            else
            {
                ViewBag.company = new BenefitHelper.Sys.Company();
            }
            int year = Convert.ToInt32(pdate.Substring(0, 4));
            int month = Convert.ToInt32(pdate.Substring(4, 2));
            int day = Convert.ToInt32(pdate.Substring(6, 2));
            ViewBag.Hpdate = pdate.Substring(0, 4) + "-" + pdate.Substring(4, 2) + "-" + pdate.Substring(6, 2);
            Common.Web.Cookie cookie = new Common.Web.Cookie();
            ViewBag.datequery = cookie.GetCookie("querydate");
            int operId = Convert.ToInt32(cookie.GetCookie("LoginUserId"));
            if (BenefitHelper.Sys.VisitPrower.HasCompanyPrower(operId, db))
            {
                BenefitHelper.Sys.TradeHistory th = new BenefitHelper.Sys.TradeHistory();
                th = th.GetSettlementModel(pdate, db);

                string js = BenefitHelper.Tools.ChartAtManager.CompanyDayBar(day, month, year, db);
                List<BenefitHelper.Data.Day.GroupDayChange> groupmonths = new BenefitHelper.Data.Day.GroupDayChange().GetCompanyDayChange(day, month, year, db);
                ViewBag.groupmonths = groupmonths;

                //周累计
                List<BenefitHelper.Data.Week.GroupWeekSum> groupweeksum = new BenefitHelper.Data.Week.GroupWeekSum().GetGroupWeekSum(th.WPdate, year, db);
                ViewBag.groupweeksum = groupweeksum;

                ////月预期
                //List<BenefitHelper.Data.Month.g> groupmonths = new BenefitHelper.Data.Day.GroupDayChange().GetCompanyDayChange(day, month, year, db);
                //ViewBag.groupmonths = groupmonths;




                BenefitHelper.Data.Day.CompanyDayChange change = new BenefitHelper.Data.Day.CompanyDayChange();
                change = change.GetCompanyDayChange(th.Id, db);//查询当日交易数据
                ViewBag.change = change;//保存当日交易数据供页面调用

                //BenefitHelper.View.OperatorAvgWeek changeweek = new BenefitHelper.View.OperatorAvgWeek().GetOperatorAvgWeek(operatorId, th.YPdate, th.WPdate, db);//查询交易员周均值
                //ViewBag.changeweek = changeweek;//保存交易员周均值数据供页面调用

                BenefitHelper.Data.Day.CompanyFiveDayAvg changefiveday = new BenefitHelper.Data.Day.CompanyFiveDayAvg().GetCompanyFiveDayAvg(th.Id, db);//五日均值
                ViewBag.changefiveday = changefiveday;//保存五日均值数据供页面调用

                BenefitHelper.Data.Month.CompanyAvgMonth changemonth = new BenefitHelper.Data.Month.CompanyAvgMonth().GetCompanyAvgMonth(th.MPdate, th.YPdate, db);//当月均值
                ViewBag.changemonth = changemonth;//保存当月均值数据供页面调用

                BenefitHelper.Data.Year.CompanyYearAvg companyyearavg = new BenefitHelper.Data.Year.CompanyYearAvg().GetCompanyAvgYear(th.YPdate, db);//查询小组均值
                ViewBag.companyyearavg = companyyearavg;//保存年均值数据供页面调用


                BenefitHelper.Data.Week.CompanyWeekSum companyweeksum = new BenefitHelper.Data.Week.CompanyWeekSum().GetCompanyWeekSum(th.YPdate, th.WPdate, db);//查询公司周累计
                ViewBag.operatorweeksum = companyweeksum;//保存公司周累计数据供页面调用

                BenefitHelper.Data.Month.CompanyMonthSum operatormonthsum = new BenefitHelper.Data.Month.CompanyMonthSum().GetCompanyMonthSum(th.MPdate, th.YPdate, db);//查询交易员月累计
                ViewBag.operatormonthsum = operatormonthsum;//保存交易员月累计数据供页面调用

                BenefitHelper.Data.Year.CompanyYearSum operatoryearsum = new BenefitHelper.Data.Year.CompanyYearSum().GetCompanyYearSum(th.YPdate, db);//查询交易员年累计
                ViewBag.operatoryearsum = operatoryearsum;//保存交易员年累计数据供页面调用

                ViewBag.js = js;
            }
            else
            {
                db.Dispose();
                return Redirect("/Error/HasNoPrower/" + uid + "/" + pdate);
            }
            return View();
        }

    }
}
